package com.abso.mp3tunes.locker.ui.properties;

import java.util.Collections;
import java.util.List;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;

/**
 * An abstract implementation of a section in a tab in the tabbed property sheet
 * page showing Locker data object properties.
 */
public class AbstractLockerPropertySection extends AbstractPropertySection {

	/**
	 * Creates the main property section control, having a two-columns grid
	 * layout.
	 * 
	 * @param parent
	 *            the parent control.
	 * @return the main section control.
	 */
	protected Composite createMainControl(Composite parent) {
		Composite mainComposite = getWidgetFactory().createComposite(parent,
				SWT.NO_FOCUS);
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 1;
		gridLayout.marginWidth = 1;
		gridLayout.marginHeight = 1;
		gridLayout.marginTop = 1;
		gridLayout.marginBottom = 1;
		mainComposite.setLayout(gridLayout);
		return mainComposite;
	}

	/**
	 * Creates a label.
	 * 
	 * @param parent
	 *            the parent control.
	 * @param text
	 *            the label's text.
	 * @return the created label.
	 */
	protected CLabel createCLabel(Composite parent, String text) {
		CLabel label = getWidgetFactory().createCLabel(parent, text);
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
		return label;
	}

	/**
	 * Creates a text.
	 * 
	 * @param parent
	 *            the parent control.
	 * @param editable
	 *            if <code>true</code> the text control is editable.
	 * @return the created text.
	 */
	protected Text createText(Composite parent, boolean editable) {
		Text text = getWidgetFactory().createText(parent, "");
		text.setEditable(editable);
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		return text;
	}

	/**
	 * Returns the single selected element.
	 * 
	 * @return the single selected element.
	 */
	protected List getSelectedElements() {
		if (!(getSelection() instanceof IStructuredSelection)) {
			return Collections.EMPTY_LIST;
		}
		return ((IStructuredSelection) getSelection()).toList();
	}

}
